package ru.urfc.portal.rest;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import ru.urfc.portal.dao.filter.Filter;
import ru.urfc.portal.dao.filter.Operator;
import ru.urfc.portal.managers.AccountingManager;
import ru.urfc.portal.managers.ClientManager;
import ru.urfc.portal.managers.NotificationManager;
import ru.urfc.portal.model.AssociationKey;
import ru.urfc.portal.model.Client;
import ru.urfc.portal.model.ClientUpdate;
import ru.urfc.portal.model.NotificationSettings;
import ru.urfc.portal.model.accounting.Contract;
import ru.urfc.portal.model.accounting.Contragent;
import ru.urfc.portal.model.accounting.InvoiceFacture;
import ru.urfc.portal.model.audit.AuditLogEntry;
import ru.urfc.portal.model.audit.DocumentAction;
import ru.urfc.portal.model.documents.DocumentType;
import ru.urfc.portal.ui.client.Constants;

@Stateless
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
@Path(Constants.CLIENTS)
public class ClientService extends BaseService {

	@EJB ClientManager clientManager;
	@EJB AccountingManager accountingManager;
	@EJB NotificationManager notificationManager;

	private static final String CLIENT_ID = "clientId";  
	
	@GET	
	public List<Client> getClients(@Context HttpServletRequest request) {
		Filter branchFilter = new Filter("code", getStr(FILTER_BRANCH_CODE, request));
		
		Filter spectrFilter = new Filter();
		spectrFilter.add("name", Operator.LIKE_IGNORE_CASE, getStr(FILTER_CLIENT_NAME, request));
		spectrFilter.add("inn", Operator.LIKE_IGNORE_CASE, getStr(FILTER_CLIENT_INN, request));
		spectrFilter.add("ogrn", Operator.LIKE_IGNORE_CASE, getStr(FILTER_CLIENT_OGRN, request));
		return clientManager.getClients(branchFilter, spectrFilter);
	}

	@Path("{" + CLIENT_ID + "}")
	public SubService getSubService() {
		return new SubService(); 
	}
	
	public class SubService {
		
		@GET
		public Client getClient(@PathParam(CLIENT_ID) Long clientId) {
			return clientManager.getClient(clientId);
		}	
		
		@PUT
		public Client updateClient(@PathParam(CLIENT_ID) Long clientId, ClientUpdate client) {
			return clientManager.updateClient(clientId, client);
		}

		@POST
		@Path(CLIENT_GENERATE_ASSOCIATION_KEY)
		public AssociationKey genereateKey(@PathParam(CLIENT_ID) Long clientId) {
			return clientManager.generateKey(clientId); 
		}
		
		@GET
		@Path(CLIENT_DETAILS)
		public Contragent getContragent(@PathParam(CLIENT_ID) Long clientId) {
			return clientManager.getContragent(clientId); 
		}
		
		
		@GET
		@Path(CONTRACTS)
		public List<Contract> getContracts(@PathParam(CLIENT_ID) Long clientId) {
			return accountingManager.getContracts(clientId);
		}
		
		@GET
		@Path(INVOICE_FACTURES)
		public List<InvoiceFacture> getInvoiceFactures(@PathParam(CLIENT_ID) Long clientId,
				@Context HttpServletRequest request) {
			Filter filter = addFilterFromToDates(new Filter(), request);
			filter.add("name", Operator.LIKE_IGNORE_CASE, getStr(FILTER_INVOICE_NAME, request));
			return accountingManager.getInvoiceFactures(clientId, filter); 
		}	
		
		@GET
		@Path(AUDIT_LOG)
		public List<AuditLogEntry> listAuditLog(@PathParam(CLIENT_ID) Long clientId) {
			return Arrays.asList(
					new AuditLogEntry(12L, new AssociatedUsersService().getUser(clientId, 123L), new Date (), 
							DocumentAction.CREATION, new DocumentType()),
					new AuditLogEntry(34L, new AssociatedUsersService().getUser(clientId, 124L), new Date (),
							DocumentAction.MODIFYING, new DocumentType())
			);
		}
		
		@GET
		@Path(CLIENT_NOTIFICATION_SETTINGS)
		public NotificationSettings getSettings(@PathParam(CLIENT_ID) Long clientId) {
			return notificationManager.getSettings(clientId);
		}

		@PUT
		@Path(CLIENT_NOTIFICATION_SETTINGS)
		public NotificationSettings updateSettings(@PathParam(CLIENT_ID) Long clientId, NotificationSettings settings) {
			return notificationManager.updateClientSettings(clientId, settings);
		}
		
	}

}
